package com.noahedus.desktopclick.game.httpdemo;

import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.noahedus.desktopclick.game.net.Contants;
import com.noahedus.desktopclick.game.net.INetClient;
import com.noahedus.desktopclick.game.net.INetDownFileCallBack;
import com.noahedus.desktopclick.game.net.INetGetCallBack;
import com.noahedus.desktopclick.game.net.INetPostCallBack;
import com.noahedus.desktopclick.game.net.INetPostFileCallBack;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


    private Button getbtn, canclebtn, postbtn, downfilebtn, postfilebtn;

    private SeekBar downseekbar;
    private TextView seektv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getbtn = findViewById(R.id.getbtn);
        postbtn = findViewById(R.id.postbtn);
        postfilebtn = findViewById(R.id.postfilebtn);
        downfilebtn = findViewById(R.id.downfilebtn);
        canclebtn = findViewById(R.id.canclebtn);
        downseekbar = findViewById(R.id.downseekbar);
        //禁用seekbar触碰
        downseekbar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //直接消费掉触摸事件
                return true;
            }
        });

        seektv = findViewById(R.id.seektv);
        downseekbar.setVisibility(View.INVISIBLE);
        seektv.setVisibility(View.INVISIBLE);
        getbtn.setOnClickListener(this);
        postbtn.setOnClickListener(this);
        postfilebtn.setOnClickListener(this);
        downfilebtn.setOnClickListener(this);
        canclebtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.getbtn:
                //get请求
                getMethod();
                break;
            case R.id.postbtn:
                //post请求
                postMethod();
                break;
            case R.id.postfilebtn:
                //上传文件请求
                postFileMethod();
                break;
            case R.id.downfilebtn:
                //下载文件
                downFileMethod();
                break;
            case R.id.canclebtn:
                //取消请求
                cancleMethod();
                break;

        }
    }

    private void postFileMethod() {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "000.jpg");
        Map<String, String> map = new HashMap<String, String>();
        map.put("username", "123");
        map.put("password", "456");

        INetClient.getsInstance()
                .getmNetManager()//得到具体网络请求实现类，这里是OkhttpNetManager
                .postFile(Contants.PostFile_IMGFile_URL, "mphone", file, map, new INetPostFileCallBack() {
                    @Override
                    public void success(String string) {
                        Log.d("print", "failed: +文件上传成功" + string);
                        //接口请求成功解析,json
                        Toast.makeText(MainActivity.this, "success: 文件上传成功", Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        Log.d("print", "failed: +文件上传失败" + throwable.getMessage());
                        //接口请求失败，打印日志
                        throwable.printStackTrace();
                    }
                }, MainActivity.this);

    }

    private void downFileMethod() {
        downseekbar.setVisibility(View.VISIBLE);
        seektv.setVisibility(View.VISIBLE);
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "测试文件.apk");
        INetClient.getsInstance()
                .getmNetManager()
                .downFile(Contants.Down_APKFile_URL, file, new INetDownFileCallBack() {
                    @Override
                    public void success(File file) {
                        //接口请求成功解析,json
                        Log.d("print", "success: 文件下载成功");
                        Toast.makeText(MainActivity.this, "success: 文件下载成功", Toast.LENGTH_LONG).show();
                        downseekbar.setVisibility(View.INVISIBLE);
                        seektv.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void progress(int progress) {
                        Log.d("print", "progress: 下载进度" + progress);
                        downseekbar.setMax(100);
                        downseekbar.setProgress(progress);
                        seektv.setText("下载进度:" + progress + "%");
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        Log.d("print", "failed: +下载失败");
                        //接口请求失败，打印日志
                        throwable.printStackTrace();
                        downseekbar.setVisibility(View.INVISIBLE);
                        seektv.setVisibility(View.INVISIBLE);
                    }
                }, MainActivity.this);
    }

    private void cancleMethod() {
        INetClient.getsInstance()
                .getmNetManager()
                .cancel(MainActivity.this);
    }

    private void getMethod() {
        INetClient.getsInstance()
                .getmNetManager()//得到具体网络请求实现类，这里是OkhttpNetManager
                .get(Contants.Get_TEMPERATURE_URL, new INetGetCallBack() {
                    @Override
                    public void success(String response) {
                        //接口请求成功解析,json
                        Toast.makeText(MainActivity.this, "success: get接口请求成功" + response, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        //接口请求失败，打印日志
                        throwable.printStackTrace();
                    }
                }, MainActivity.this);
    }

    private void postMethod() {

        Map<String, String> headmap = new HashMap<String, String>();

        Map<String, String> map = new HashMap<String, String>();
        map.put("weaid", "1");
        map.put("date", "2018-08-13");
        map.put("appkey", "10003");
        map.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
        map.put("format", "json");

        INetClient.getsInstance()
                .getmNetManager()//得到具体网络请求实现类，这里是OkhttpNetManager
                .post(Contants.Post_TEMPERATURE_URL, headmap, map, new INetPostCallBack() {
                    @Override
                    public void success(String response) {
                        //接口请求成功解析,json
                        Toast.makeText(MainActivity.this, "success: post接口请求成功" + response, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        //接口请求失败，打印日志
                        throwable.printStackTrace();
                    }
                }, MainActivity.this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        cancleMethod();
    }
}
